/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */

function DynamicButtonsUI_Clear(sActionsPanel)
{
	try
	{
		var divMainLayoutPanelActions = document.getElementById(sActionsPanel);
		//alert('DynamicButtonsUI_Clear(' + sActionsPanel + ')' + divMainLayoutPanelActions);
		if ( divMainLayoutPanelActions != null && divMainLayoutPanelActions.childNodes != null )
		{
			while ( divMainLayoutPanelActions.childNodes.length > 0 )
			{
				divMainLayoutPanelActions.removeChild(divMainLayoutPanelActions.firstChild);
			}
		}
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'DynamicButtonsUI_Clear');
	}
}

function DynamicButtonsUI_LoadButtons(sLayoutPanel, sActionsPanel, layout, row, Page_Command)
{
	try
	{
		var divActionsPanel = document.getElementById(sActionsPanel);
		var pnlDynamicButtons = document.createElement('div');
		pnlDynamicButtons.id        = 'pnlDynamicButtons';
		pnlDynamicButtons.className = 'button-panel';
		divActionsPanel.appendChild(pnlDynamicButtons);
		
		for ( var nLayoutIndex in layout )
		{
			var lay = layout[nLayoutIndex];
			var sCONTROL_TYPE       = lay.CONTROL_TYPE      ;
			//var sMODULE_NAME        = lay.MODULE_NAME       ;
			//var sMODULE_ACCESS_TYPE = lay.MODULE_ACCESS_TYPE;
			//var sTARGET_NAME        = lay.TARGET_NAME       ;
			//var sTARGET_ACCESS_TYPE = lay.TARGET_ACCESS_TYPE;
			var sCONTROL_TEXT       = lay.CONTROL_TEXT      ;
			var sCONTROL_TOOLTIP    = lay.CONTROL_TOOLTIP   ;
			var sCONTROL_CSSCLASS   = lay.CONTROL_CSSCLASS  ;
			var sTEXT_FIELD         = lay.TEXT_FIELD        ;
			var sARGUMENT_FIELD     = lay.ARGUMENT_FIELD    ;
			var sCOMMAND_NAME       = lay.COMMAND_NAME      ;
			var sURL_FORMAT         = lay.URL_FORMAT        ;
			var sURL_TARGET         = lay.URL_TARGET        ;
			var sONCLICK_SCRIPT     = lay.ONCLICK_SCRIPT    ;
			
			var sCONTROL_ID = '';
			if ( !Sql.IsEmptyString(sCOMMAND_NAME) )
			{
				sCONTROL_ID = 'btnDynamicButtons_' + sCOMMAND_NAME;
			}
			else if ( !Sql.IsEmptyString(sCONTROL_TEXT) )
			{
				sCONTROL_ID = 'btnDynamicButtons_' + sCONTROL_TEXT;
				if ( sCONTROL_TEXT.indexOf('.') >= 0 )
				{
					sCONTROL_ID = sCONTROL_TEXT.split('.')[1];
					sCONTROL_ID = sCONTROL_ID.replace('LBL_', '');
					sCONTROL_ID = sCONTROL_ID.replace('_BUTTON_LABEL', '');
				}
			}
			if ( !Sql.IsEmptyString(sCONTROL_ID) )
			{
				//sCONTROL_ID = sCONTROL_ID.Trim();
				sCONTROL_ID = sCONTROL_ID.replace(' ', '_');
				sCONTROL_ID = sCONTROL_ID.replace('.', '_');
			}
			try
			{
				var arrTEXT_FIELD = new Array();
				var objTEXT_FIELD = new Array();
				if ( sTEXT_FIELD != null )
				{
					arrTEXT_FIELD = sTEXT_FIELD.split(' ');
					objTEXT_FIELD = arrTEXT_FIELD;
					for ( var i = 0 ; i < arrTEXT_FIELD.Length; i++ )
					{
						if ( arrTEXT_FIELD[i].length > 0 )
						{
							objTEXT_FIELD[i] = '';
							if ( rdr != null )
							{
								if ( rdr[arrTEXT_FIELD[i]] != null )
									objTEXT_FIELD[i] = rdr[arrTEXT_FIELD[i]];
							}
						}
					}
				}
				if ( sCONTROL_TYPE == 'Button' )
				{
					var btn = document.createElement('input');
					btn.type            = 'submit';
					if ( !Sql.IsEmptyString(sCONTROL_ID) )
						btn.id              = sCONTROL_ID;
					btn.value           = '  ' + L10n.Term(sCONTROL_TEXT) + '  ';
					btn.title           = (sCONTROL_TOOLTIP.length > 0) ? L10n.Term(sCONTROL_TOOLTIP) : '';
					btn.className       = sCONTROL_CSSCLASS;
					btn.CommandName     = sCOMMAND_NAME;
					//btn.OnClientClick   = sONCLICK_SCRIPT;
					btn.style.marginRight = '3px';
					var sARGUMENT_VALUE = null;
					if ( sARGUMENT_FIELD != null && sARGUMENT_FIELD.length > 0 )
					{
						if ( rdr[sARGUMENT_FIELD] != null )
						{
							sARGUMENT_VALUE = rdr[sARGUMENT_FIELD];
							btn.CommandArgument = sARGUMENT_VALUE;
						}
					}
					//btn.onclick = new Function('function("' + sLayoutPanel + '", "' + sCOMMAND_NAME + '", "' + sARGUMENT_VALUE + '")');
					btn.onclick = BindArguments(Page_Command, sLayoutPanel, sActionsPanel, sCOMMAND_NAME, sARGUMENT_VALUE);
					pnlDynamicButtons.appendChild(btn);
				}
				else if ( sCONTROL_TYPE == 'HyperLink' )
				{
					var lnk = document.createElement('a');
					pnlDynamicButtons.appendChild(lnk);
					if ( !Sql.IsEmptyString(sCONTROL_ID) )
						lnk.id              = sCONTROL_ID;
					lnk.innerHTML       = L10n.Term(sCONTROL_TEXT);
					lnk.toolTip         = (sCONTROL_TOOLTIP.length > 0) ? L10n.Term(sCONTROL_TOOLTIP) : '';
					lnk.className       = sCONTROL_CSSCLASS;
					//lnk.href            = String_Format(sURL_FORMAT, objTEXT_FIELD);
					//btn.Command        += Page_Command;
					btn.CommandName     = sCOMMAND_NAME;
					//btn.OnClientClick   = sONCLICK_SCRIPT;
					lnk.style.marginRight = '3px';
					lnk.style.marginLeft  = '3px';
				}
				else if ( sCONTROL_TYPE == 'ButtonLink' )
				{
					var btn = document.createElement('input');
					btn.type            = 'submit';
					if ( !Sql.IsEmptyString(sCONTROL_ID) )
						btn.id              = sCONTROL_ID;
					btn.value           = '  ' + L10n.Term(sCONTROL_TEXT) + '  ';
					btn.title           = (sCONTROL_TOOLTIP.length > 0) ? L10n.Term(sCONTROL_TOOLTIP) : '';
					btn.className       = sCONTROL_CSSCLASS;
					btn.CommandName     = sCOMMAND_NAME;
					//if ( sONCLICK_SCRIPT != null && sONCLICK_SCRIPT.length > 0 )
					//	btn.OnClientClick   = String.Format(sONCLICK_SCRIPT, objTEXT_FIELD);
					//else
					//	btn.OnClientClick   = "window.location.href='" + Sql.EscapeJavaScript(String_Format(sURL_FORMAT, objTEXT_FIELD)) + "'; return false;";
					btn.style.marginRight = '3px';
					//btn.onclick = new Function('function("' + sLayoutPanel + '", "' + sCOMMAND_NAME + '", null)');
					btn.onclick = BindArguments(Page_Command, sLayoutPanel, sActionsPanel, sCOMMAND_NAME, null);
					pnlDynamicButtons.appendChild(btn);
				}
			}
			catch(e)
			{
				SplendidError.SystemAlert(e, 'DynamicButtonsUI_LoadButtons ' + sCONTROL_TEXT);
			}
		}
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'DynamicButtonsUI_LoadButtons ' + sCONTROL_TEXT);
	}
}

function DynamicButtonsUI_Load(sLayoutPanel, sActionsPanel, sVIEW_NAME, row, Page_Command, callback)
{
	try
	{
		var bgPage = chrome.extension.getBackgroundPage();
		bgPage.DynamicButtons_LoadLayout(sVIEW_NAME, function(status, message)
		{
			if ( status == 1 )
			{
				// 10/03/2011 Paul.  DynamicButtons_LoadLayout returns the layout. 
				var layout = message;
				try
				{
					DynamicButtonsUI_Clear(sActionsPanel)
					//var layout = bgPage.SplendidCache.DynamicButtons(sVIEW_NAME);
					DynamicButtonsUI_LoadButtons(sLayoutPanel, sActionsPanel, layout, row, Page_Command)
					
					callback(1, null);
				}
				catch(e)
				{
					callback(-1, SplendidError.FormatError(e, 'DynamicButtonsUI_Load'));
				}
			}
			else
			{
				callback(status, message);
			}
		});
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'DynamicButtonsUI_Load'));
	}
}

